<?php
namespace app\admin\controller;
use think\Db;
class Product extends Base{
    public function product_list(){
        $keyword=input('get.keyword');
        $state=input('get.state');
        $hot=input('get.hot');
        $new=input('get.new');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $map = [
            ['time', '>', $fromtime],
            ['time', '<', $totime]
        ];
        if($state==0){
            array_push($map,['state','<',3]);
        }else{
            array_push($map,['state','=',$state]);
        }
        if($new==0){
            array_push($map,['new','<',3]);
        }else{
            array_push($map,['new','=',$new]);
        }
        if($hot==0){
            array_push($map,['hot','<',3]);
        }else{
            array_push($map,['hot','=',$hot]);
        }

        $list=Db::name('product')->where($map)->where('title','like',"%{$keyword}%")->order('id desc')->paginate(10);
        $lists=Db::name('product')->where($map)->where('title','like',"%{$keyword}%")->select();
        $this->assign('keyword',input('get.keyword'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('new',$new);
        $this->assign('hot',$hot);
        $this->assign('state',$state);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    //子分类
    public function subCates()
    {
        $res=Db::name('cates')->where('f_id',$_GET["f_id"])->where('state',1)->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '获取失败！']);
        }
    }
    public function product_add(){
        if(request()->isGet()){
            $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
            $subcates=Db::name('cates')->where('f_id',$cates[0]['id'])->where('state',1)->select();
            $this->assign('subcates',$subcates);
            $this->assign('cates',$cates);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['c_id']=$param['c_id'];
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            $data['imgs']=$param['imgs'];
            $data['detail']=html_entity_decode($param['detail']);
            $data['sort']=$param['sort'];
            $data['hot']=1;
            $data['new']=1;
            $data['state']=1;
            $data['time']=time();
            $data['uptime']=time();
            Db::startTrans();
            try {
                $p_id=Db::name('product')->insertGetId($data);
                // if($p_id){
                    for($i=0;$i<count($param['list']);$i++){
                        $data2['spec']=$param['list'][$i]['color'];
                        $data2['p_id']=$p_id;
                        $data2['sort']=$i;
                        $specid=Db::name('spec')->insertGetId($data2);
                        for($k=0;$k<count($param['list'][$i]['ggarr']);$k++){
                            $data3['spec_val']=$param['list'][$i]['ggarr'][$k]['guigebig'];
                            $data3['spec_id']=$specid;
                            $data3['sort']=$k;
                            $data3['p_id']=$p_id;
                            Db::name('spec_val')->insert($data3);
                        }
                    }
                    for($j=0;$j<count($param['menu']);$j++){
                        $data4['combination']='';
                        for($l=0;$l<(count($param['menu'][$j]['biao'])-2);$l++){
                                $data4['combination'] .=$param['menu'][$j]['biao'][$l]['inpval'].",";
                        }
                        $data4['combination']=substr($data4['combination'],0,strlen($data4['combination'])-1); 
                        $count=count($param['menu'][$j]['biao'])-1;
                        $count2=count($param['menu'][$j]['biao'])-2;
                        $data4['money']=$param['menu'][$j]['biao'][$count]['inpval'];
                        $data4['num']=$param['menu'][$j]['biao'][$count2]['inpval'];
                        $data4['p_id']=$p_id;
                        Db::name('spec_combination')->insert($data4);
                    }
                //     return json(['code' => 200,'msg' => '成功']);
                // }else{
                //     return json(['code' => 500,'msg' => '失败']);
                // }

                // 提交事务
                Db::commit();
                if($p_id){
                    return json(['code' => 200, 'msg' => '成功！']);
                }else{
                    return json(['code' => 500, 'msg' => '提交事物失败！']);
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 500, 'msg' => '事物回滚失败！']);
            }
        }
    }
    public function goodsInfo()
    {
        $param = input('post.');
        $type=Db::name('spec')->where('p_id',$param['id'])->where('state',1)->order('id asc')->select();
        $list=Db::name('spec_val')->where('p_id',$param['id'])->where('state',1)->order('id asc')->select();
        $data2=array();
        for($i=0;$i<count($type);$i++){
            $data1=array();
            for($k=0;$k<count($list);$k++){
                if($type[$i]['id']==$list[$k]['spec_id']){
                  $data1[]=array(
                    'id'=>$list[$k]['id'],
                    'name'=>$list[$k]['spec_val']
                    );
                }           
            }
            $data2[]=array(
                'id'=>$type[$i]['id'],
                'sepc_name'=>$type[$i]['spec'],
                'sepc_val'=>$data1
            );
        }
        $combination=Db::name('spec_combination')->where('p_id',$param['id'])->where('state',1)->order('id asc')->select();
        $res['spec']=$data2;
        $res['combination']=$combination;
        echo json_encode($res);
    }
    public function product_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
            $data=Db::name('product')->where('id',$param['id'])->find();
            if($data['imgs']){
                $data['imgsarr']=explode(',',$data['imgs']);
            }
            $cate_one_id=Db::name('cates')->where('id',$data['c_id'])->where('state',1)->value('f_id');
            $subcates=Db::name('cates')->where('f_id',$cate_one_id)->where('state',1)->select();


            $type=Db::name('spec')->where('p_id',$param['id'])->where('state',1)->select();
            $list=Db::name('spec_val')->where('p_id',$param['id'])->where('state',1)->select();
            $data2=array();
            for($i=0;$i<count($type);$i++){
                $data1=array();
                for($k=0;$k<count($list);$k++){
                    if($type[$i]['id']==$list[$k]['spec_id']){
                      $data1[]=array(
                        'id'=>$list[$k]['id'],
                        'name'=>$list[$k]['spec_val']
                        );
                    }           
                }
                $data2[]=array(
                    'id'=>$type[$i]['id'],
                    'sepc_name'=>$type[$i]['spec'],
                    'sepc_val'=>$data1
                );
            }
            $combination=Db::name('spec_combination')->where('p_id',$param['id'])->where('state',1)->select();
            $res['spec']=$data2;
            $res['combination']=$combination;

            $this->assign('subcates',$subcates);
            $this->assign('cates',$cates);
            $this->assign('data',$data);
            $this->assign('cate_one_id',$cate_one_id);
            $this->assign('res',json_encode($res));
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            
            $data['c_id']=$param['c_id'];
            $data['title']=$param['title'];
            $data['img']=$param['img'];
            if(substr($param['imgs'],0,1)==','){
                $data['imgs']=substr($param['imgs'],1);
            }else{
                $data['imgs']=$param['imgs'];
            }
            $data['detail']=html_entity_decode($param['detail']);
            $data['sort']=$param['sort'];
            $data['uptime']=time();
            Db::startTrans();
            try {
                $res=Db::name('product')->where(array('id'=>$param['id']))->update($data);
                if(isset($param['scid'])){
                    Db::name('spec_combination')->where(array('p_id'=>$param['id']))->update(['state'=>3]);
                    for($t=0;$t<count($param['scid']);$t++){
                        Db::name('spec')->where(array('id'=>$param['scid'][$t]['id']))->update(['state'=>3]);
                        Db::name('spec_val')->where(array('spec_id'=>$param['scid'][$t]['id']))->update(['state'=>3]);
                    }  
                }
                if(isset($param['smallid'])){
                    Db::name('spec_combination')->where(array('p_id'=>$param['id']))->update(['state'=>3]);
                    for($y=0;$y<count($param['smallid']);$y++){
                        Db::name('spec_val')->where(array('id'=>$param['smallid'][$y]['id']))->update(['state'=>3]);
                    
                    }  
                }
                for($i=0;$i<count($param['list']);$i++){
                    if(isset($param['list'][$i]['coid'])){
                        Db::name('spec')->where(array('id'=>$param['list'][$i]['coid']))->update(['spec'=>$param['list'][$i]['color']]);
                    }else{
                        Db::name('spec_combination')->where(array('p_id'=>$param['id']))->update(['state'=>3]);
                        $specid=Db::name('spec')->insertGetId(array('spec'=>$param['list'][$i]['color'],'p_id'=>$param['id'],'sort'=>$i));
                    }
                    
                    for($j=0;$j<count($param['list'][$i]['ggarr']);$j++){
                        if(is_numeric($param['list'][$i]['ggarr'][$j]['shuid'])){
                            Db::name('spec_val')->where(array('id'=>$param['list'][$i]['ggarr'][$j]['shuid']))->update(['spec_val'=>$param['list'][$i]['ggarr'][$j]['guigebig']]);
                        }else{
                            if($param['list'][$i]['coid']){
                                $spec_id=$param['list'][$i]['coid'];
                            }else{
                                $spec_id=$specid;
                            }
                            Db::name('spec_combination')->where(array('p_id'=>$param['id']))->update(['state'=>3]);
                            Db::name('spec_val')->insert(array('spec_val'=>$param['list'][$i]['ggarr'][$j]['guigebig'],'spec_id'=>$spec_id,'sort'=>$j,'p_id'=>$param['id']));
                        }
                    }
                }
                for($k=0;$k<count($param['menu']);$k++){
                    $data4['combination']='';
                    for($l=0;$l<(count($param['menu'][$k]['biao'])-2);$l++){
                            $data4['combination'] .=$param['menu'][$k]['biao'][$l]['inpval'].",";
                    }
                    $data4['combination']=substr($data4['combination'],0,strlen($data4['combination'])-1); 
                    $count=count($param['menu'][$k]['biao'])-1;
                    $count2=count($param['menu'][$k]['biao'])-2;
                    $data4['money']=$param['menu'][$k]['biao'][$count]['inpval'];
                    $data4['num']=$param['menu'][$k]['biao'][$count2]['inpval'];
                    $data4['p_id']=$param['id'];
                    if(isset($param['menu'][$k]['id'])){
                        Db::name('spec_combination')->where(array('id'=>$param['menu'][$k]['id']))->update($data4);
                    }else{
                        Db::name('spec_combination')->insert($data4);
                    }
                }

                // 提交事务
                Db::commit();
                if($res){
                    return json(['code' => 200, 'msg' => '成功！']);
                }else{
                    return json(['code' => 500, 'msg' => '提交事物失败！']);
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 500, 'msg' => '事物回滚失败！']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function new_off(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['new'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function new_on(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['new'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function hot_off(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['hot'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function hot_on(){
        $id=input('param.id');
        $status = Db::name('product')->where(array('id'=>$id))->update(['hot'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function product_del(){
        $param = input('post.');
        $res= Db::name('product')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    // 原图上传
    public function uploadtmds(){
        $photo='';
        $imgs=[];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        if(isset($_FILES['file'])){
            move_uploaded_file($_FILES['file']['tmp_name'],$imgpath.$imgsubpath.$_FILES['file']['name']);
            $photo="/uploads/images/".$imgsubpath.$_FILES['file']['name'];
        }
        if(isset($_FILES['imgs'])){
            for ($i=0; $i <count($_FILES['imgs']['name']) ; $i++) {
                move_uploaded_file($_FILES['imgs']['tmp_name'][$i],$imgpath.$imgsubpath.$_FILES['imgs']['name'][$i]);
                $imgs[$i]="/uploads/images/".$imgsubpath.$_FILES['imgs']['name'][$i];
            }
        }
        return json(['code' => 200, 'photo' =>$photo,'imgs' =>implode(",",$imgs), 'msg' => '成功']);
    }
}
